﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace MODULE_KETOAN
{
    public partial class frm_rpt_tonghopthumua : Form
    {
        ketoanDataContext context = cls_ketoan.getcontext();
        public frm_rpt_tonghopthumua()
        {
            InitializeComponent();
        }

        private void btPrint_Click(object sender, EventArgs e)
        {


            try
            {
                string tungay, denngay;
                string d, m, y;

                if (this.dtptungay.Value.Day >= 10)
                {
                    d = this.dtptungay.Value.Day.ToString();
                }
                else
                {
                    d = "0" + this.dtptungay.Value.Day.ToString();
                }

                if (this.dtptungay.Value.Month >= 10)
                {
                    m = this.dtptungay.Value.Month.ToString();
                }
                else
                {
                    m = "0" + this.dtptungay.Value.Month.ToString();
                }
                y = this.dtptungay.Value.Year.ToString();
                tungay = d + "/" + m + "/" + y;
                if (this.dtpdenngay.Value.Day >= 10)
                {
                    d = this.dtpdenngay.Value.Day.ToString();
                }
                else
                {
                    d = "0" + this.dtpdenngay.Value.Day.ToString();
                }

                if (this.dtpdenngay.Value.Month >= 10)
                {
                    m = this.dtpdenngay.Value.Month.ToString();
                }
                else
                {
                    m = "0" + this.dtpdenngay.Value.Month.ToString();
                }
                y = this.dtpdenngay.Value.Year.ToString();
                denngay = d + "/" + m + "/" + y;

                string connectstring = context.Connection.ConnectionString;
                SqlConnection connection = new SqlConnection(connectstring);
                SqlCommand command = new SqlCommand();
                command.Connection = connection;
                command.CommandText = "KT_rpt_tonghopthumuanguyenlieutheongay'" + tungay + "','" + denngay + "'";

                SqlDataAdapter adapter = new SqlDataAdapter(command);
                DataTable tb = new DataTable();
                adapter.Fill(tb);

                //IEnumerable<KT_rpt_tonghopthumuanguyenlieutheongayResult> lst = context.KT_rpt_tonghopthumuanguyenlieutheongay(tungay, denngay).AsEnumerable();


                MODULE_TIENICH.cls_report objReport = new MODULE_TIENICH.cls_report();
                MODULE_TIENICH.cls_tienich cls = new MODULE_TIENICH.cls_tienich();
                //DataTable dt = cls.IEnumerabletoDataTable(lst);

                Cursor.Current = Cursors.WaitCursor;
                objReport.Load_Report(Application.StartupPath + "\\reports\\tonghopthumuanguyenlieutheongay.rpt", tb);
                //objReport.Load_SubReport("Sub_BienBanNhap_VTNPL_BTP", objCtrl.LoadPrint_BienBanNhap_L)
                objReport.Formulas_Report("tungay", "'" + tungay + "'");
                objReport.Formulas_Report("denngay", "'" + denngay + "'");
                objReport.Open_Report();
                //objCtrl = Nothing
                //objReport = Nothing
                Cursor.Current = Cursors.Default;


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btExcel_Click(object sender, EventArgs e)
        {
            this.saveFileDialog1.Filter = "Excel file(*.xls)|*.xls";
            this.saveFileDialog1.FileName = "tonghopthumuanguyenlieutheongay.xls";
            if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                Cursor.Current = Cursors.WaitCursor;

                try
                {
                    string tungay, denngay;
                    string d, m, y;

                    if (this.dtptungay.Value.Day >= 10)
                    {
                        d = this.dtptungay.Value.Day.ToString();
                    }
                    else
                    {
                        d = "0" + this.dtptungay.Value.Day.ToString();
                    }

                    if (this.dtptungay.Value.Month >= 10)
                    {
                        m = this.dtptungay.Value.Month.ToString();
                    }
                    else
                    {
                        m = "0" + this.dtptungay.Value.Month.ToString();
                    }
                    y = this.dtptungay.Value.Year.ToString();
                    tungay = d + "/" + m + "/" + y;
                    if (this.dtpdenngay.Value.Day >= 10)
                    {
                        d = this.dtpdenngay.Value.Day.ToString();
                    }
                    else
                    {
                        d = "0" + this.dtpdenngay.Value.Day.ToString();
                    }

                    if (this.dtpdenngay.Value.Month >= 10)
                    {
                        m = this.dtpdenngay.Value.Month.ToString();
                    }
                    else
                    {
                        m = "0" + this.dtpdenngay.Value.Month.ToString();
                    }
                    y = this.dtpdenngay.Value.Year.ToString();
                    denngay = d + "/" + m + "/" + y;



                    List<KT_rpt_tonghopthumuanguyenlieutheongayResult> lst = context.KT_rpt_tonghopthumuanguyenlieutheongay(tungay, denngay).ToList();

                    grdata.Rows.Clear();

                    if (lst != null)
                    {

                        for (int i = 0; i < lst.Count(); i++)
                        {
                            KT_rpt_tonghopthumuanguyenlieutheongayResult ele = lst.ElementAt(i);
                            grdata.Rows.Add();
                            grdata.Rows[i].Cells["ngay"].Value = ele.ngay;
                            grdata.Rows[i].Cells["tenhang"].Value = ele.tenhang;
                            grdata.Rows[i].Cells["trongluongquychuan"].Value = ele.tlquychuan;
                            grdata.Rows[i].Cells["gianhap"].Value = ele.gianhap;
                            grdata.Rows[i].Cells["thanhtien"].Value = ele.thanhtien;


                        }
                    }
                    String filePath = saveFileDialog1.FileName;

                    ExcelWrite myExcel = new ExcelWrite();
                    List<String> list = myExcel.DGVToExcel(grdata);
                    //String filePath = path + "\\BangKeThanhToan.xls";

                    myExcel.WriteDateToExcel(filePath, list, "A1", "E1", "A2");
                    MessageBox.Show("Export dữ liệu thành công ...", "Thông báo");

                    Cursor.Current = Cursors.Default;

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

        }

        private void frm_rpt_tonghopthumua_Load(object sender, EventArgs e)
        {
        

        }

        private void btExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        
    }
}
